package pww.action;
import Util.ActionContext;
import a.annotation.Component;
import a.annotation.Inject;
import anntation.Mapping;
import pww.dto.ValueDto;
import pww.entity.Administrator;
import pww.entity.Student;
import pww.exception.LoginException;
import pww.exception.NofindException;
import pww.service.AdministratorService;
import pww.service.StudentService;
import view.ForwardView;
import view.JsonView;
import view.RedircView;
import view.ViewResult;
import javax.servlet.http.HttpSession;
import javax.swing.text.View;
import java.util.List;

@Component("AdminController")
public class AdminController {
    @Inject("loginAdminServiceImpl")
    private AdministratorService administratorService;
    @Inject("studentServiceImpl")
    private StudentService studentService;
    //管理员登录页面
    @Mapping("a")
    public ViewResult login(String account,String password){
        try {
            Administrator login = administratorService.isLogin(account,password);
            HttpSession session = ActionContext.getContext().getRequest().getSession();
            session.setAttribute("admin",login);
            //跳转到首页的控制器
            return new RedircView("page");
        }catch (LoginException e){
           return new RedircView("login.jsp");
        }
    }
    //跳转到管理系统的首页
    @Mapping("page")
    public ViewResult page(){
        List<Student> students = studentService.queryAllStudent();
        ActionContext.getContext().getRequest().setAttribute("student",students);
        //跳转到管理系统的首页
        return new ForwardView("page.jsp");
    }
    //跳转到修改信息的页面
    @Mapping("updataView")
    public ViewResult queryStudent(Integer id){
            Student student = administratorService.select(id);
            ActionContext.getContext().getRequest().setAttribute("s",student);
            return new ForwardView("update.jsp");
    }
    //管理员修改信息提交
    @Mapping("update")
    public ViewResult update(Student student){
        administratorService.updateStudent(student);
        //修改成功就跳转到首页
        return new RedircView("page");
    }

    //删除学生信息的提交
    @Mapping("delete")
    public ViewResult delete(Integer id){
        administratorService.deleteStudent(id);
        return new RedircView("page");
    }
}
